package com.tanhao.takeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhao.takeaway.commom.R;
import com.tanhao.takeaway.entity.category;
import com.tanhao.takeaway.service.categoryService;
import com.tanhao.takeaway.service.serviceImpl.categoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class categoryController {
    @Autowired
    private categoryService service;

    @PostMapping
    public R<String> save(@RequestBody category c){
        service.save(c);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize){
        Page page1=new Page(page,pageSize);
        LambdaQueryWrapper<category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(category::getSort);
        service.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }

    @DeleteMapping
    public R<String> deleteById(Long ids){
        service.remove(ids);
        return R.success("删除用户成功");
    }

    @PutMapping
    public R<String> update(@RequestBody  category c){
        boolean b = service.updateById(c);

        return R.success("修改用户成功");
    }

    @GetMapping("/list")
    public R<List<category>> list(category c){
        LambdaQueryWrapper<category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(c.getType()!=null,category::getType,c.getType());
        lambdaQueryWrapper.orderByAsc(category::getSort);
        lambdaQueryWrapper.orderByDesc(category::getUpdateTime);
        List<category> li = service.list(lambdaQueryWrapper);
        return R.success(li);
    }
}
